<template>
  <div class="fixed size-full p-0 m-0 inset-0">
    <div>
      <YkMenu v-if="display1" :options="template" :user="user" :height="30">
      </YkMenu>
    </div>
    <YkContent :task-bar-height="0" :menu-bar-height="30">
      <router-view v-slot="{ Component }">
        <component ref="child" :is="Component"></component>
      </router-view> <!-- 这里将渲染当前路由对应的组件 -->
    </YkContent>
  </div>

</template>

<script setup lang="ts">
import { router } from './router'
import { YkMenu, YkContent, utils, useYklibLoginStore } from 'yklib'
import { computed, inject, onMounted, ref } from 'vue'
import { Emitter } from 'mitt'
import { storeToRefs } from 'pinia'
import { includes } from 'lodash-es'


const mitt: Emitter<any> | undefined = inject('mitt')

let child = ref()
const store = useYklibLoginStore()
const { user, projectName } = storeToRefs(store)

const display1 = computed(() => {// 当是登录页面时，菜单和任务栏不显示，否则显示
  return router.currentRoute.value.name && router.currentRoute.value.name !== 'Login'
})

onMounted(() => {
  const url = new URL(window.location.href) // 获取当前浏览器地址的url
  if (includes(url.search, 'pid')) {
    console.log('跳转至首页')
    window.location.href = 'https://sges.yangke.site:20003'
  }
})

const goto = (routerPath: string) => {
  if (routerPath === '/') {
    router.push(routerPath)
  } else {
    projectName.value = routerPath.replace('/', '');
    console.log(`跳转到${routerPath}`)
    router.push(routerPath)
  }
}

const template = [
  {
    label: '文件',
    submenu: [
      {
        label: '打开...',
        key: '打开',
        click: () => {
          utils.importData((str: string) => {
            if (!str) { // 点击了取消按钮
              return
            }
            let proj = JSON.parse(str)
            let currentPage = proj['currentPage'] || 'SgesCalA'
            if (currentPage === 'EbsFlow') {
              mitt?.emit('loadGraphData', proj)
            } else {
              localStorage.setItem('storeSgesCalA', proj.storeSgesCalA) // 将数据恢复到localStorage
              localStorage.setItem('storeSgesCalC', proj.storeSgesCalC) // 将数据恢复到localStorage
              router.push(currentPage) // 跳转到对应页面
              localStorage.setItem('currentPage', currentPage)
              location.reload()  // 刷新页面，加载localStorage中的数据
            }
          }, ['.ebsFlow', '.txt', '.dat', '.json', '.csv'])
        }   // 点击事件，需要传入函数对象，electron可以在此处指定点击事件
      },
      {
        type: 'separator'
      },
      {
        label: '保存到本地...',
        key: '保存到本地',
        click: () => {
          const routeName = router.currentRoute.value.name
          if (routeName === 'EbsFlow') { // 保存EbsFlow数据
            mitt?.emit('saveGraphData')
          } else if (child.value.persistData) {
            child.value.persistData()
            let _1 = { 'storeSgesCalC': localStorage.getItem('storeSgesCalC') }
            _1['storeSgesCalA'] = localStorage.getItem('storeSgesCalA')
            _1['currentPage'] = localStorage.getItem('currentPage')
            let json_str = JSON.stringify(_1)
            utils.save_file(json_str)
          }
        }
      },
      {
        label: '清除缓存',
        key: '清除缓存',
        click: () => {
          localStorage.clear()
        }
      },
      {
        label: '关于',
        key: '关于',
        click: () => {
          localStorage.setItem('currentPage', 'about')
          goto('about')
        }
      },
      {
        label: '退出',
        role: 'quit'
      }
    ]
  },
  {
    label: '工具',
    submenu:
      [
        {
          label: '塔式重力储能设计(方案1)',
          key: 'SgesCalA',
          click: () => {
            localStorage.setItem('currentPage', 'SgesCalA') // currentPage必须和key相同，根据该值跳转页面
            goto('/SgesCalA')
          } // 鼠标点击事件
        },
        {
          label: '塔式重力储能设计(方案3)',
          key: 'SgesCalC',
          click: () => {
            localStorage.setItem('currentPage', 'SgesCalC')
            goto('SgesCalC')
          }
        },
        {
          label: '山体重力储能设计(方案1)',
          key: 'MountainGesCalA',
          click: () => {
            localStorage.setItem('currentPage', 'MountainGesCalA')
            goto('MountainGesCalA')
          }
        },
        {
          label: '不确定度分析',
          key: 'Uncertainty',
          click: () => {
            localStorage.setItem('currentPage', 'Uncertainty')
            goto('Uncertainty')
          }
        },
        {
          label: 'excel数据',
          key: 'ExcelViewer',
          click: () => {
            localStorage.setItem('currentPage', 'ExcelViewer')
            goto('ExcelViewer')
          }
        },
        {
          label: 'Canvas',
          key: 'Canvas',
          click: () => {
            localStorage.setItem('currentPage', 'Canvas')
            goto('Canvas')
          }
        },
        {
          label: 'EbsFlow',
          key: 'EbsFlow',
          click: () => {
            localStorage.setItem('currentPage', 'EbsFlow')
            router.push('EbsFlow')
          }
        },
        // {
        //   label: 'Video',
        //   key: 'Video',
        //   click: () => {
        //     localStorage.setItem("currentPage", "Video")
        //     goto('Video')
        //   }
        // },
        {
          type: 'separator'
        },
        {
          label: '储能系统成本计算',
          key: '储能系统成本计算',
          click: () => {
            localStorage.setItem('currentPage', 'Canvas')

          }
        },
        {
          label: '储能塔架设计',
          key: '储能塔架设计',
          click: () => {

          }
        },
        {
          label: '承托结构设计',
          key: '承托结构设计',
          click: () => {

          }
        },
        {
          label: '钢丝绳强度计算',
          key: '钢丝绳强度计算',
          click: () => {

          }
        },
        {
          label: '轴系强度计算',
          key: '轴系强度计算',
          click: () => {

          }
        },
        {
          label: '网盘（ipv4）',
          key: 'diskV4',
          click: () => {
            window.open('http://sges.yangke.site:20010', '_blank')
          }
        },
        {
          label: '网盘（ipv6）',
          key: 'diskV6',
          click: () => {
            window.open('http://disk.yangke.site:20010', '_blank')
          }
        }
      ]
  }
  ,
  {
    label: '查询',
    submenu:
      [
        {
          label: '全国各省峰谷电价信息',
          key: '全国各省峰谷电价信息',
          click: () => {
            localStorage.setItem('currentPage', 'pricePower')
            goto('pricePower')
          }
        },
        {
          label: '储能系统成本计算',
          key: '储能系统成本计算',
          click: () => {
            goto('debug')
          }
        },
        {
          label: '调试信息',
          key: 'debug',
          click: () => {
            localStorage.setItem('currentPage', 'debug')
            goto('debug')
          }
        }

      ]
  }
]

</script>


<style scoped lang="postcss"></style>
